﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Model.Entity;
using Model.Entity.Members;
using System.Collections.Generic;

namespace LibraryManagerTests.Model.Entity
{

    /// <summary>
    /// Class containing unit tests for Book class and Member's subclasses.
    /// </summary>
    [TestClass]
    public class EntityTests
    {

        /// <summary>
        /// Tests Member descendants for proper management of borrowing addition and removal.
        /// </summary>
        [TestMethod]
        public void TestMemberBorrowings()
        {
            Member[] members = { new ForeignUniMember(), 
                                 new LocalTeacher(), 
                                 new LocalStudent(), 
                                 new Other()};

            foreach (Member member in members) 
            {
                Borrowing borrowing1 = new Borrowing(null);
                Borrowing borrowing2 = new Borrowing(null);
                Borrowing borrowing3 = new Borrowing(null);

                member.AddBorrowing(borrowing1);
                Assert.AreEqual(borrowing1, member.Borrowings[0]);
                Assert.AreEqual(1, member.Borrowings.Count);

                member.RemoveBorrowing(borrowing1);
                Assert.AreEqual(0, member.Borrowings.Count);

                member.AddBorrowing(borrowing1);
                member.AddBorrowing(borrowing1);
                Assert.AreEqual(borrowing1, member.Borrowings[0]);
                Assert.AreEqual(1, member.Borrowings.Count);

                member.RemoveBorrowing(borrowing1);
                Assert.AreEqual(0, member.Borrowings.Count);

                member.AddBorrowing(borrowing1);
                member.AddBorrowing(borrowing2);
                Assert.AreEqual(borrowing1, member.Borrowings[0]);
                Assert.AreEqual(borrowing2, member.Borrowings[1]);
                Assert.AreEqual(2, member.Borrowings.Count);

                member.RemoveBorrowing(borrowing1);
                Assert.AreEqual(borrowing2, member.Borrowings[0]);
                Assert.AreEqual(1, member.Borrowings.Count);
                member.RemoveBorrowing(borrowing2);
                Assert.AreEqual(0, member.Borrowings.Count);

                member.AddBorrowing(borrowing1);
                member.AddBorrowing(borrowing2);
                member.RemoveBorrowing(borrowing1);
                member.AddBorrowing(borrowing3);
                Assert.AreEqual(borrowing3, member.Borrowings[1]);
                Assert.AreEqual(borrowing2, member.Borrowings[0]);
                Assert.AreEqual(2, member.Borrowings.Count);

                member.RemoveBorrowing(borrowing3);
                member.RemoveBorrowing(borrowing2);
                Assert.AreEqual(0, member.Borrowings.Count);
            }
        }

        /// <summary>
        /// Tests Book class for proper management of borrowing addition and removal.
        /// </summary>
        [TestMethod]
        public void TestBookBorrowings()
        {
            Book book = new Book(null);

            Borrowing borrowing1 = new Borrowing(null);
            Borrowing borrowing2 = new Borrowing(null);
            Borrowing borrowing3 = new Borrowing(null);

            book.AddBorrowing(borrowing1);
            Assert.AreEqual(borrowing1, book.Borrowings[0]);
            Assert.AreEqual(1, book.Borrowings.Count);

            book.RemoveBorrowing(borrowing1);
            Assert.AreEqual(0, book.Borrowings.Count);

            book.AddBorrowing(borrowing1);
            book.AddBorrowing(borrowing1);
            Assert.AreEqual(borrowing1, book.Borrowings[0]);
            Assert.AreEqual(1, book.Borrowings.Count);

            book.RemoveBorrowing(borrowing1);
            Assert.AreEqual(0, book.Borrowings.Count);

            book.AddBorrowing(borrowing1);
            book.AddBorrowing(borrowing2);
            Assert.AreEqual(borrowing1, book.Borrowings[0]);
            Assert.AreEqual(borrowing2, book.Borrowings[1]);
            Assert.AreEqual(2, book.Borrowings.Count);

            book.RemoveBorrowing(borrowing1);
            Assert.AreEqual(borrowing2, book.Borrowings[0]);
            Assert.AreEqual(1, book.Borrowings.Count);
            book.RemoveBorrowing(borrowing2);
            Assert.AreEqual(0, book.Borrowings.Count);

            book.AddBorrowing(borrowing1);
            book.AddBorrowing(borrowing2);
            book.RemoveBorrowing(borrowing1);
            book.AddBorrowing(borrowing3);
            Assert.AreEqual(borrowing3, book.Borrowings[1]);
            Assert.AreEqual(borrowing2, book.Borrowings[0]);
            Assert.AreEqual(2, book.Borrowings.Count);

            book.RemoveBorrowing(borrowing3);
            book.RemoveBorrowing(borrowing2);
            Assert.AreEqual(0, book.Borrowings.Count);
        }
    }
}
